package com.yanqu.road.server.logic.thread;

import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.ChatModule;
import com.yanqu.road.server.manger.ForbiddenWordMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.manger.thirdparty.ThirdPartyMgr;
import com.yanqu.road.server.pb.WorldMessagePb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.executor.GeneralTask;

public class LocalForbiddenChatTask extends GeneralTask {
    private GamePlayer player;
    private int type;
    private int contentType;
    private String content;
    private int activityId;
    private String banquetId;
    private long time;
    boolean replace;

    public LocalForbiddenChatTask(GamePlayer player, int type, int contentType, String content, int activityId, String banquetId, long time, boolean replace) {
        super();
        this.player = player;
        this.type = type;
        this.contentType = contentType;
        this.content = content;
        this.activityId = activityId;
        this.banquetId = banquetId;
        this.time = time;
        this.replace = replace;
    }

    @Override
    public void doRun() {
        // 发送一条成功发送消息
        WorldMessageProto.WorldChatRespMsg.Builder respMsg = WorldMessageProto.WorldChatRespMsg.newBuilder();
        respMsg.setRet(0);
        player.sendPacket(Protocol.U_WORLD_CHAT, respMsg);

        WorldMessageProto.WorldMessageTempMsg.Builder msg = WorldMessagePb.parseWorldMessageTempMsg(player.getUserInfo(), type, contentType, content, time, activityId, banquetId);
        UserInfo userInfo = player.getUserInfo();
        long nowTime = System.currentTimeMillis();
        if (userInfo.getForbiddenSpeakEndTime() > nowTime) {//被禁言
            forbidden(msg);
            return;
        }

        ThirdPartyMgr.uploadChatDataGetState(player, content, (int) (time / 1000), type, activityId, banquetId);

        if (ForbiddenWordMgr.isForbidden(content)) {
            // 有屏蔽字
            forbidden(msg);
        } else {
            // 发送成功
            WorldMessageMgr.addWorldMessage(player, msg);
        }
    }

    public void forbidden(WorldMessageProto.WorldMessageTempMsg.Builder msg) {
        if (replace) {
            msg.setContent("***");
        }
        ChatModule chatModule = player.getModule(ChatModule.class);
        chatModule.addForbiddenMessage(msg);
        player.sendPacket(Protocol.U_WORLD_MESSAGE_SYNC, msg);
    }
}
